import React, {Component} from 'react';
import { Modal} from 'antd';
import PropTypes from 'prop-types';
import '../../assets/stylus/com/feedback/modal.styl';
import {initModals} from "../../state/action/comAction";


class scModal extends Component {

  constructor (props) {
    super();
    this.props = props;
  }

  static contextTypes = {
    store: PropTypes.object.isRequired
  }

  componentWillMount () {
    this.initStore();
  }

  initStore () {
    const store = this.context.store;
    this.refreshStore(store);
    store.subscribe(() => {
      this.refreshStore(store);
    });
  }

  refreshStore (store) {
    const modalInfo = store.getState().modalInfo;
    this.setState({
      visible: modalInfo.visible,
      param: modalInfo.param,
      fetchParams: modalInfo.fetchParams,
      context: modalInfo.context
    });
  }

  onCancel () {
    this.context.store.dispatch(initModals(!1));
  }

  render() {
    return (
      this.context.store.getState().modalInfo.key === this.props.config.key && <Modal
        width={this.props.config.width}
        title={this.props.config.title}
        visible={this.state.visible}
        onCancel={this.onCancel.bind(this)}
        wrapClassName="vertical-center-modal"
        footer={null}
        // confirmLoading={this.props.confirmLoading}
      >
        {this.props.config.content}
      </Modal>
    );
  }
}

export default scModal;

scModal.propsType = {
  config: PropTypes.object
}